package com.cloud.template.base.converter;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.util.Date;

/**
 * 日期转换
 * Created by Viole on 2017/10/6.
 */
public class DateConverter implements Converter<String,Date> {
    private static Logger logger = LogManager.getLogger(DateConverter.class);

    @Override
    public Date convert(String dateformat) {
        Date result=null;
        if(StringUtils.isNoneBlank(dateformat)){
            try {
                result= DateUtils.parseDate(dateformat, new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm"});
            } catch (ParseException e) {
                logger.error("时间格式错误",e);
            }
        }
        return result;
    }
}
